from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
from utils import parallel_line_strip, texture_coords_1d
from numpy import array, empty
from numpy.linalg import norm

RED = array((255,0,0))
YELLOW = array((255,255,0))
GREEN = array((0,255,0))
		
class RoadViewBase(object):
	def __init__(self):
		self.strip=[(0,0),(100,100),(200,50),(300,150),(400,120)]
		self.strip=[array(p, dtype="float") for p in self.strip]

	def Draw(self, select=False):
		strip = self.strip
		pstrip = parallel_line_strip(strip,2)
		texcoord = texture_coords_1d(strip)
		img=array((123,244,233,123,0,3,0,123,123),dtype="uint8")
		glTexImage1D(GL_TEXTURE_1D,0,GL_RGB,len(img)/3,0,GL_RGB,GL_UNSIGNED_BYTE,img)
		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
		#glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
		#glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
		glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL)

		glEnable(GL_TEXTURE_1D)
		glBegin(GL_QUAD_STRIP)
		#glBegin(GL_LINE_STRIP)
		glColor3f(1.0, 0.0, 0.0)
		for i in range(len(strip)):
			glTexCoord1f(texcoord[i])
			glVertex2fv(strip[i])
			glVertex2fv(pstrip[i])
		glEnd()
		glDisable(GL_TEXTURE_1D)

class RoadView(object):
	def __init__(self, model):
		self.model = model
		self.bbox = model.bbox

	def Draw(self, select=False):
		for linkid in self.model.linkids():
			link = self.model.links[linkid]
			strip = self.model.centerlines[linkid]
			pstrip = parallel_line_strip(strip,2)
			texcoord = texture_coords_1d(strip)
			img=self.build_texture(link.snapshot(),link.vehicle_per_cell)
			glTexImage1D(GL_TEXTURE_1D,0,GL_RGB,len(img)/3,0,GL_RGB,GL_UNSIGNED_BYTE,img)
			glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
			glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
			glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
			#glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
			#glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
			glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL)
			glEnable(GL_TEXTURE_1D)
			
			glBegin(GL_QUAD_STRIP)
			#glBegin(GL_LINE_STRIP)
			glColor3f(1.0, 0.0, 0.0)
			for i in range(len(strip)):
				glTexCoord1f(texcoord[i])
				glVertex2fv(strip[i])
				glVertex2fv(pstrip[i])
			glEnd()
			glDisable(GL_TEXTURE_1D)

	def build_texture(self, vnlist, vmax):
		colors = []
		#print vnlist, vmax
		for vn in vnlist:
			k = float(vn)/float(vmax)
			color = RED*k+GREEN*(1.0-k)
			color = color/norm(color)*255.0
			for i in range(10):
				colors.append(color)

		texture = empty(len(colors)*3,dtype="uint8")
		for i in range(len(colors)):
			texture[i*3:(i+1)*3] = colors[i]
		#print texture
		return texture
	
class ConnectorView(object):
	def __init__(self, model):
		self.model = model
		self.bbox = model.bbox

	def Draw(self, select=False):
		for id in self.model.connectorids():
			link = self.model.connectors[id]
			strip = self.model.centerlines[id]
			pstrip = parallel_line_strip(strip,2)
			
			glBegin(GL_QUAD_STRIP)
			glColor3f(0.0, 0.0, 1.0)
			for i in range(len(strip)):
				glVertex2fv(strip[i])
				glVertex2fv(pstrip[i])
			glEnd()
